package com.rpc.server;

import io.vertx.core.Vertx;

public class VertxHttpServer implements HttpServer{

    public void doStart(int port) {
        //1.创建Vert.x实例
        Vertx vertx = Vertx.vertx();

        //2.创建HTTP服务
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();
        //3.监听HTTP端口并处理请求
        server.requestHandler(new HttpServerHandler());
//        server.requestHandler(request->{
//           //处理HTTP请求
//            System.out.println("Received request:" + request.method()+ " " +
//                    request.uri());
//            //发送HTTP响应
//            request.response()
//                    .putHeader("content-type", "text/plain")
//                    .end("Hello from Vert.x HTTP server!");
//        });
        //4.启动HTTP服务器病监听制定的端口
        server.listen(port, result->{
            if(result.succeeded()) {
                System.out.println("Server is now listening on port:" + port);
            }else {
                System.err.println("Failed to start server:" + result.cause());
            }
        });
    }
}
